import { Ipc_ChannelEnum } from "@enum/channel.enum";
import { Ipc } from "@main/helper/ipcMain";
import path from "path";

export class PathService {
  /**
   * 返回当前文件系统的分隔符
   */
  static sep(): "\\" | "/" {
    return path.sep;
  }

  /**
   * 判断是否为绝对路径
   */
  static isAbsolute(url: string): boolean {
    return path.isAbsolute(url);
  }

  /**
   * 返回当前路径的最后一段
   * @param url 路径
   * @param ext 文件扩展名 如果传则把最后一段的文件扩展名过滤掉
   */
  static baseName(url: string, ext?: string): string {
    return path.basename(url, ext);
  }

  /**
   * 返回目录名
   */
  static dirName(url: string): string {
    return path.dirname(url);
  }

  /**
   * 返回路径最后一段的扩展名
   */
  static extName(url: string): string {
    return path.extname(url);
  }

  /**
   * 规范路径
   */
  static normalize(url: string): string {
    return path.normalize(url);
  }

  static on(): void {
    
    Ipc.handle(Ipc_ChannelEnum.PATH_SEP, (_) => {
      return this.sep();
    });

    Ipc.handle<[string]>(Ipc_ChannelEnum.PATH_IS_ABSOLUTE, (_, url) => {
      return this.isAbsolute(url);
    });

    Ipc.handle<[string]>(Ipc_ChannelEnum.PATH_BASENAME, (_, url) => {
      return this.baseName(url);
    });

    Ipc.handle<[string]>(Ipc_ChannelEnum.PATH_DIRNAME, (_, url) => {
      return this.dirName(url);
    });

    Ipc.handle<[string]>(Ipc_ChannelEnum.PATH_EXTNAME, (_, url) => {
      return this.extName(url);
    });

    Ipc.handle<[string]>(Ipc_ChannelEnum.PATH_NORMALIZE, (_, url) => {
      return this.normalize(url);
    });
  }
}
